<?php
	defined('_JEXEC') or die(); 
    JHTML::_('behavior.tooltip'); 
    $listOrder    = @$this->lists['order'];
    $listDirn     = @$this->lists['order_Dir'];
	$db = &JFactory::getDbo();
	$user = &JFactory::getUser();
?>
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.css" />
<link rel="stylesheet" type="text/css" href="<?php echo JURI::root(); ?>js/multiselect/jquery-ui.css" />
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/jquery-ui.min.js"></script>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/multiselect/jquery.multiselect.js"></script>
<script type="text/javascript" src="<?php echo JURI::base(true)?>/components/com_wrd/assets/js/jquery.autocomplete.js"></script>
<link href="<?php echo JURI::base(true)?>/components/com_wrd/assets/css/jquery.autocomplete.css" type="text/css" rel="stylesheet" />
<style>
	img.calendar {float: right;}
    #tbl_filter{
        margin: 0 ; padding: 0 ;
    }
    #tbl_filter td{
        padding: 3px;
    }
    #tbl_filter input, #tbl_filter img {
        float: left;
        margin: 0 5px 0 0;
        width: auto;
    }
	.ac_over strong {color: #FFFFFF !important; cursor: pointer;}
	.svoucher {
		cursor: pointer;
		-moz-box-shadow:inset 0px 1px 0px 0px #dcecfb;
		-webkit-box-shadow:inset 0px 1px 0px 0px #dcecfb;
		box-shadow:inset 0px 1px 0px 0px #dcecfb;
		background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #bddbfa), color-stop(1, #80b5ea) );
		background:-moz-linear-gradient( center top, #bddbfa 5%, #80b5ea 100% );
		filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#bddbfa', endColorstr='#80b5ea');
		background-color:#bddbfa;
		-moz-border-radius:6px;
		-webkit-border-radius:6px;
		border-radius:6px;
		border:1px solid #84bbf3;
		display:inline-block;
		color:#ffffff;
		font-family:arial;
		font-size:12px;
		font-weight:bold;
		padding:4px 10px;
		text-decoration:none;
		text-shadow:1px 1px 0px #528ecc;
}.svoucher:hover {
	background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #80b5ea), color-stop(1, #bddbfa) );
	background:-moz-linear-gradient( center top, #80b5ea 5%, #bddbfa 100% );
	filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#80b5ea', endColorstr='#bddbfa');
	background-color:#80b5ea;
}.svoucher:active {
	position:relative;
	top:1px;
}
	a.svoucher:hover {text-decoration: none;}
    #extra_filter_field { width: 114px; }
    table.adminlist tbody tr td { padding: 8px 8px; }
    table.adminlist thead th { padding: 8px; }
	div.loading {width: 20px;float: left;}
</style>
<script type="text/javascript">
    function submitform(pressbutton){
        var form = document.adminForm ;
        if (pressbutton)
        {
            form.task.value=pressbutton ;
        }
        if ((pressbutton=='send_redemp_notice_email')||(pressbutton=='send_redemp_pick_up_notice')){
              form.controller.value="reports" ;
        }
        form.submit();
    }
	jQuery(function(){
		
		jQuery("#sredem").autocomplete("<?php echo JRoute::_("index.php?option=com_wrd&controller=reports&task=getCodeRedem", false)?>", {
		    width: 210,
		    matchContains: true,
		    minChars: 1,
		    selectFirst: false,
		});
		
		jQuery("#member_id").autocomplete("<?php echo JRoute::_("index.php?option=com_wrd&controller=reports&task=getMemberid", false)?>", {
		    width: 210,
		    matchContains: true,
		    minChars: 1,
		    selectFirst: false,
		});
		
		jQuery("#code_voucher").autocomplete("<?php echo JRoute::_("index.php?option=com_wrd&controller=reports&task=getVouchercode", false)?>", {
		    width: 210,
		    matchContains: true,
		    minChars: 1,
		    selectFirst: false,
		});
		
		var status = <?php echo isset($this->status)?$this->status: '-1'; ?>;
		jQuery(".status").val(status);
	});	
</script>
<form action="<?php echo JRoute::_("index.php?option=com_wrd&controller=reports")?>" method="post" name="adminForm" id="adminForm" >
<div class="row-fluid">
    <?php if (!empty( $this->sidebar)) : ?>
	<div id="j-sidebar-container" class="span2">
		<?php echo $this->sidebar; ?>
	</div>
	<div id="j-main-container" class="span10">
	<?php else : ?>
		<div id="j-main-container">
	<?php endif;?>
		<br clear="all">	
    	<div class="alert">   
                <table style='margin-bottom:5px' id="tbl_filter">
                    <tbody>
                        <tr>
                            <td align="left" valign="top" style="">
                                <table >
                                    <tr>
                                        <td>
                                        	<strong>Redemption Code</strong><br>
                                            <input value="<?php echo $this->sredem; ?>" type="text" id="sredem" name="sredem" style="width: 210px;height: 22px;border-radius:4px;" />
                                        </td>
                                        <td>
                                        	<strong>Member ID</strong><br>
                                            <input value="<?php echo $this->member_id; ?>" type="text" id="member_id" name="member_id" style="width: 210px;height: 22px;border-radius:4px;" />
                                        </td>
                                        <td>
                                        	<strong>Voucher Code</strong><br>
                                            <input value="<?php echo $this->code_voucher; ?>" type="text" id="code_voucher" name="code_voucher" style="width: 210px;height: 22px;border-radius:4px;" />
                                        </td>
                                    </tr>
                                </table>                                
                            </td>                             
                             <td align="left" valign="top" id="statistic_by_job" style="">
                                <table>                                  
                                    <tr>
                                       
                                        <td>Status:<br>
                                           <select class="status" name="status" id="status">
                                                <option value="-1">All</option>
												<option value="1">Used Voucher</option>
												<option value="0">Unused voucher</option>
												<option value="9">Cancel Voucher</option>
                                            </select>
                                        </td>
										<td><input type="submit" name="view" onclick="document.getElementById('task').value = 'approvevoucher';" value="View" /></td>
                                    </tr>                                                                                                                                       
                                </table>
								
                            </td>                    
                        </tr>
                    </tbody>
                </table>
                <input type="hidden" name="controller" value="reports" />
                <input type="hidden" name="option" value="com_wrd" />
		</div>
<script type="text/javascript" src="<?php echo JURI::root(); ?>js/dialog/jquery-ui-1.8.20.custom.min.js"></script>
<script type="text/javascript">
    jQuery(function(){
		jQuery("a.svoucher").click(function(){
			var id = jQuery(this).attr('rel');
			jQuery("#save_"+id).hide();
			var outlet_name = jQuery("#outlet_"+id).find("option:selected").text();
			var outlet_voucher = jQuery("#outlet_"+id).find("option:selected").val();
			var date = jQuery("#date"+id).val();
			if(outlet_voucher == 0){alert("Please chose outlet");return false;}
			if(date == 0){alert("Please chose date use outlet");return false;}
			else {
				jQuery("#loading_"+id).show();
				jQuery.ajax({
					url: "index.php?option=com_wrd&controller=reports&task=saveVoucher",
					type: "POST",
					data: 'id='+id+'&outlet_name='+outlet_name+'&outlet_voucher='+outlet_voucher+'&date='+date,
					success: function (res) {
						if(res) {
							jQuery("#loading_"+id).hide();
							jQuery("#save_"+id).show().text("Saved").css("color","blue");
							
						}
						else {
							alert("Error save voucher");
							jQuery("#save_"+id).show().text("error").css("color","red");
						}
					}		
				});
			}
		});
        
    });
</script>
<h3 class="filter-detail"><span style="color:#C00; float:right;margin-top: 15px;">Total: <?php echo $this->pagination->total; ?></span></h3>
<table class="adminlist extra-adminlist table">
  <thead>
    <tr class="extra-adminlist-tr" >
        <th nowrap="nowrap"><strong>Order</strong></th>                   
		<th nowrap="nowrap"><strong>Member ID</strong></th>
		<th nowrap="nowrap"><strong>Product Name</strong></th>
		<th nowrap="nowrap"><strong>Redemption Code</strong></th>	
		<th nowrap="nowrap"><strong>Voucher Code</strong></th>
		<th nowrap="nowrap"><strong>Expired Voucher</strong></th>
		<th nowrap="nowrap"><strong>Date Use Voucher</strong></th>		
		<th nowrap="nowrap"><strong>Outlet Use Voucher</strong></th>
		<th nowrap="nowrap"><strong>Action</strong></th>
    </tr>
  </thead>
  <tbody>
    <?php 
        if(!empty($this->items)):
            $index = 0;
            $i=0;

			foreach($this->items as $key => $item):
				$product_name= unserialize($item->productName);
                $i++;
                echo '<tr class="row'.(($i+1)%2).'" valign="top">';
                    echo '<td  style="width: 5%;">'.$i.'</td>';  
					echo '<td  style="width: 5%;">'.$item->member_id.'</td>';  	
					echo '<td  style="width:25%">'.$product_name['en-GB'].'</td>';
					echo '<td  style="width:15%">'.$item->code_redemption.'</td>';	
					echo '<td style="text-align:right;">'.$item->code_voucher .'</td>';
					echo '<td style="text-align:right;">';
						
						echo empty($item->expired) ? '' : date("d-m-Y",strtotime($item->expired));
					echo '</td>';
					echo '<td style="width: 14%;text-align:right;">'.JHTML::_ ( 'calendar', ($item->date_voucher != '') ? $item->date_voucher : '', 'date'.$item->id, 'date'.$item->id, '%Y-%m-%d').'</td>';
					echo '<td style="text-align:right;">';
					echo "<select name='outlet_".$item->id."' id='outlet_".$item->id."'>";
					echo "<option value='0' selected>Please chose outel </option>";	
					
					foreach($this->outlet as $k => $v){
						echo "<option value='".$v->value."'";
						echo ($v->value == $item->outlet_voucher)?'selected':'';
						echo ">$v->text</option>";
					}
					echo "</select>";
					echo '</td>';
					echo '<td style="text-align:right;width: 15%;">';
				?>	
					<div class="loading" id="loading_<?php echo $item->id;?>" style="display: none;float:left: width: 20px;"><img src="<?php echo JURI::root();?>images/loading.gif" style="margin-top: 0;" /></div>
					<span id="save_<?php echo $item->id;?>" style="margin-right: 8px;background: #FFF;display:none;"></span>
					<a class="svoucher svoucher_<?php echo $item->outlet ?>" rel="<?php echo $item->id ?>"  name="svoucher" onclick="">Save Voucher <a/>	
				
				<?php	
					echo '</td>';
					
                echo '</tr>';
                
            endforeach;
        endif;
    ?>
    
  </tbody>
  <tfoot>
        <tr>
            <td colspan="16">
                <?php
                     echo $this->pagination->getListFooter();
                ?>
            </td>
        </tr>
    </tfoot>
</table>
	<input type="hidden" name="controller" value="reports" />
	<input type="hidden" name="option" value="com_wrd" />
	<input type="hidden" name="task" value="approvevoucher" />
	<input type="hidden" name="layout" value="approvevoucher" />
	<?php echo JHTML::_( 'form.token' ); ?>
	</div>
</div>
 </form>